import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/system/dynamicFormField'

const api = {

    /**
     * 新增动态表单字段
     * @param dynamicFormField
     * @returns {Promise<*|undefined>}
     */
    createDynamicFormField: (dynamicFormField) =>
      axiosClient().post(`${requestUrl}/addInfo`, dynamicFormField),

    /**
     * 根据ID删除动态表单字段
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteDynamicFormFieldById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新动态表单字段
     * @param id
     * @param dynamicFormField
     * @returns {Promise<*|undefined>}
     */
    updateDynamicFormFieldFull: (id, dynamicFormField) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, dynamicFormField),

    /**
     * 根据ID部分更新动态表单字段
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateDynamicFormFieldPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询动态表单字段
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getDynamicFormFieldById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询动态表单字段
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getDynamicFormFieldPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询动态表单字段
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getDynamicFormFieldList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.formId) {
            params.append('formId', businessParam.formId)
        }
        if (businessParam.fieldName) {
            params.append('fieldName', businessParam.fieldName)
        }
        if (businessParam.fieldLabel) {
            params.append('fieldLabel', businessParam.fieldLabel)
        }
        if (businessParam.fieldType) {
            params.append('fieldType', businessParam.fieldType)
        }
        if (businessParam.fieldDictId) {
            params.append('fieldDictId', businessParam.fieldDictId)
        }
        if (businessParam.required) {
            params.append('required', businessParam.required)
        }
        if (businessParam.defaultValue) {
            params.append('defaultValue', businessParam.defaultValue)
        }
        if (businessParam.maxLength) {
            params.append('maxLength', businessParam.maxLength)
        }
        if (businessParam.viewRoleIds) {
            params.append('viewRoleIds', businessParam.viewRoleIds)
        }
        if (businessParam.editRoleIds) {
            params.append('editRoleIds', businessParam.editRoleIds)
        }
        if (businessParam.hidden) {
            params.append('hidden', businessParam.hidden)
        }
        if (businessParam.componentType) {
            params.append('componentType', businessParam.componentType)
        }
        if (businessParam.componentProps) {
            params.append('componentProps', businessParam.componentProps)
        }
        if (businessParam.frameProps) {
            params.append('frameProps', businessParam.frameProps)
        }
        if (businessParam.validationRules) {
            params.append('validationRules', businessParam.validationRules)
        }
        if (businessParam.dependentFields) {
            params.append('dependentFields', businessParam.dependentFields)
        }
        if (businessParam.width) {
            params.append('width', businessParam.width)
        }
        if (businessParam.align) {
            params.append('align', businessParam.align)
        }
        if (businessParam.helpText) {
            params.append('helpText', businessParam.helpText)
        }
        if (businessParam.searchable) {
            params.append('searchable', businessParam.searchable)
        }
        if (businessParam.listShow) {
            params.append('listShow', businessParam.listShow)
        }
        if (businessParam.slotName) {
            params.append('slotName', businessParam.slotName)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
